home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 4
/
Apprentice-Release4.iso
/
Source Code
/
C
/
Frameworks
/
Grant's CGI Framework 1.0b12
/
Util
/
MemoryUtil.c
< prev
next >
Wrap
Text File
|
1995-12-09
|
3KB
|
219 lines
/*****
*
* MemoryUtil.c
*
* This is a support file for "Grant's CGI Framework".
* Please see the license agreement that accompanies the distribution package
* for licensing details.
*
* Copyright ©1995 by Grant Neufeld
* grant@acm.com
* http://arpp1.carleton.ca/grant/
*
*****/
#include "MyConfiguration.h"
#include "compiler_stuff.h"
#include "globals.h"
#include "MemoryUtil.h"
/*** MEMORY ALLOCATION ***/
/* I use these 'wrappers' to toolbox memory calls to provide a way
to consistantly handle error conditions. */
Handle
MyNewHandle ( long theSize, OSErr *outErr )
{
Handle theHandle;
OSErr theErr;
theHandle = NewHandle ( theSize );
theErr = MemError ();
if ( outErr != nil )
{
*outErr = theErr;
}
if ( theErr != noErr )
{
return nil;
}
else
{
return theHandle;
}
}
Handle
MyNewHandleClear ( long theSize, OSErr *outErr )
{
Handle theHandle;
OSErr theErr;
theHandle = NewHandleClear ( theSize );
theErr = MemError ();
if ( outErr != nil )
{
*outErr = theErr;
}
if ( theErr != noErr )
{
return nil;
}
else
{
return theHandle;
}
}
Ptr
MyNewPtr ( long theSize, OSErr *outErr )
{
Ptr thePtr;
OSErr theErr;
thePtr = NewPtr ( theSize );
theErr = MemError ();
if ( outErr != nil )
{
*outErr = theErr;
}
if ( theErr != noErr )
{
return nil;
}
else
{
return thePtr;
}
}
Ptr
MyNewPtrClear ( long theSize, OSErr *outErr )
{
Ptr thePtr;
OSErr theErr;
thePtr = NewPtrClear ( theSize );
theErr = MemError ();
if ( outErr != nil )
{
*outErr = theErr;
}
if ( theErr != noErr )
{
return nil;
}
else
{
return thePtr;
}
}
/*** EMERGENCY MEMORY ***/
#pragma mark -
/* IM-Memory: 1-46 */
Boolean
InitializeEmergencyMemory ( OSErr *outErr )
{
Boolean success;
OSErr theErr;
success = true;
gEmergencyMemory = NewHandle ( kMemCushionSize );
theErr = MemError ();
if ( outErr != nil )
{
*outErr = theErr;
}
if ( theErr != noErr )
{
gEmergencyMemory = nil;
success = false;
}
else
{
SetGrowZone ( NewGrowZoneProc(MyGrowZone) );
}
return success;
} /* InitializeEmergencyMemory */
/* IM-Memory: 1-48 */
pascal long
MyGrowZone ( Size cbNeeded )
{
OSErr theErr;
long theA5;
long theSize;
theA5 = SetCurrentA5 ();
if ( *gEmergencyMemory && ( gEmergencyMemory != GZSaveHnd() ) )
{
EmptyHandle ( gEmergencyMemory );
theErr = MemError ();
if ( theErr != noErr )
{
theSize = nil;
}
else
{
theSize = kMemCushionSize; /* kEmergencyMemorySize in IM */
}
}
else
{
theSize = nil;
}
theA5 = SetA5 ( theA5 );
return theSize;
} /* MyGrowZone */
/* To check that the memory reserve is intact.
Returns true if there is emergency memory on reserve.
IM-Memory: 1-47 */
Boolean
IsEmergencyMemAvail ( void )
{
return ( (gEmergencyMemory != nil) && (*gEmergencyMemory != nil) );
} /* IsEmergencyMemAvail */
/* IM-Memory: 1-48 */
#if !(kCompileWithQuitOnLowMemory)
OSErr
RecoverEmergencyMemory ( void )
{
ReallocateHandle ( gEmergencyMemory, kMemCushionSize );
return MemError ();
} /* RecoverEmergencyMemory */
#endif
/*** EOF ***/